﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Data;
using ProgNetComponents.Data.SqlCe;

namespace MiniKsiegowosc.DbUpdate
{
    public class DbUpdateClass
    {
        public static void AddError(string name, string error)
        {
            string s = "INSERT INTO DB_UPDATE_ERRORS (DB_UPERR_DATA, DB_UPERR_NAZWA, DB_UPERR_ERROR) VALUES (GETDATE(), " + name.Substring(0, Math.Min(name.Length, 50)).EscapeSql() + "," + error.Substring(0, Math.Min(error.Length, 1000)).EscapeSql() + ")";
            manager.ExecuteNonQuery(s);
        }

        public List<DBUpdateProc> proc = new List<DBUpdateProc>();
        static SqlCeDataManager manager = new SqlCeDataManager();
        public void UpdateAll()
        {
            DBScriptProc s = new BeforeUpdateScripts();
            s.ExecuteError += new EventHandler<ScriptExecuteErrorEventArgs>(s_ExecuteError);
            s.Manager = manager;
            int i = manager.ExecuteScalar("SELECT STP_OP_WARTOSC FROM STP_OPCJE WHERE STP_OP_NAZWA = 'DB_BEFORE_UPDATE'").AsInt();
            s.StartFrom = i;
            s.Execute();
            int o = -1;
             o = manager.ExecuteNonQuery("UPDATE STP_OPCJE SET STP_OP_WARTOSC = " + s.ScriptsCount + " WHERE STP_OP_NAZWA = 'DB_BEFORE_UPDATE'");
             if (o <= 0)
                 manager.ExecuteNonQuery("INSERT INTO STP_OPCJE(STP_OP_NAZWA, STP_OP_WARTOSC) VALUES ('DB_BEFORE_UPDATE', " + s.ScriptsCount + ")");

            proc.Add(new UpdateKpirGroups());
            proc.Add(new UpdateVatSGroups());
            proc.Add(new UpdateVatZGroups());

            foreach (DBUpdateProc p in proc)
            {
                p.Manager = manager;
                p.RowUpdateError += new EventHandler<UpdateRowEventArgs>(p_RowUpdateError);
                bool res = p.Execute();
            }

            s = new AfterUpdateScripts();
            s.ExecuteError +=new EventHandler<ScriptExecuteErrorEventArgs>(s_ExecuteError);
            s.Manager = manager;
            i = manager.ExecuteScalar("SELECT STP_OP_WARTOSC FROM STP_OPCJE WHERE STP_OP_NAZWA = 'DB_AFTER_UPDATE'").AsInt();
            s.StartFrom = i;
            s.Execute();
            o = -1;
            o = manager.ExecuteNonQuery("UPDATE STP_OPCJE SET STP_OP_WARTOSC = " + s.ScriptsCount + " WHERE STP_OP_NAZWA = 'DB_AFTER_UPDATE'");
            if (o <= 0)
                manager.ExecuteNonQuery("INSERT INTO STP_OPCJE(STP_OP_NAZWA, STP_OP_WARTOSC) VALUES ('DB_AFTER_UPDATE', " + s.ScriptsCount + ")");

        }

        void p_RowUpdateError(object sender, UpdateRowEventArgs e)
        {
            string name = sender.GetType().Name;
            DBUpdateProc p = sender as DBUpdateProc;
            foreach (string col in p.KeyKolumns)
                name += ", [" + col + "] = " + e.Row[col].AsString();
            AddError(name, manager.LastException != null ? manager.LastException.Message : "");
        }

        void s_ExecuteError(object sender, ScriptExecuteErrorEventArgs e)
        {
            AddError(sender.GetType().Name + " [" + e.Index + "]", manager.LastException != null ?manager.LastException.Message : "");
        }
    }
}
